package com.gm.wholesale.module.custom.ui.customManger;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.views.superview.SuperTextView;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.custom.model.CustomModel;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.util.ViewUtil;

import java.util.HashMap;

public class CreateCustomFragment extends BaseFragment implements View.OnClickListener {
    private SuperTextView cancleBtn;
    private SuperTextView subMitBtn;
    private TextView titleTv;
    private EditText custormNameEd;
    private EditText contactNameEd;
    private EditText phoneEd;
    private EditText addressEd;

    private CustomBean mCustomBean;

    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_create_custorm, parent);
        initView(view);
        if (getArguments()!=null && getArguments().getSerializable("data")!=null){
            mCustomBean= (CustomBean) getArguments().getSerializable("data");
            showView();
        }

    }
    private void showView(){
        if (mCustomBean!=null){
            custormNameEd.setText(mCustomBean.getName());
            contactNameEd.setText(mCustomBean.getContactPerson());
            phoneEd.setText(mCustomBean.getContactPhone());
            addressEd.setText(mCustomBean.getAddress());
            titleTv.setText("编辑客户");
        }
    }

    private void initView(View view) {
        titleTv=view.findViewById(R.id.titleTv);
        cancleBtn = view.findViewById(R.id.cancleBtn);
        subMitBtn = view.findViewById(R.id.subMitBtn);
        custormNameEd = view.findViewById(R.id.custormNameEd);
        contactNameEd = view.findViewById(R.id.contactNameEd);
        phoneEd = view.findViewById(R.id.phoneEd);
        addressEd = view.findViewById(R.id.addressEd);
        cancleBtn.setOnClickListener(this);
        subMitBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.cancleBtn) {
            finishUI();
        } else if (view.getId() == R.id.subMitBtn) {
           createCustorm();
        }
    }

    private void createCustorm(){
         String name=custormNameEd.getText().toString();
         String contactName=contactNameEd.getText().toString();
         String phoneNumber=phoneEd.getText().toString();
         String address=addressEd.getText().toString();
         if (TextUtils.isEmpty(name)){
             ToastUtil.showToast("请输入客户名称");
             return;
         }
        HashMap<String,Object> data=new HashMap<>();
         data.put("name",name);
        data.put("contactPerson",contactName);
        data.put("contactPhone",phoneNumber);
        data.put("address",address);
         //修改
        if (mCustomBean!=null){
            data.put("version",mCustomBean.getVersion());
            CustomModel.updateCustorm((IBaseView) getActivity(),mCustomBean.getId(), data, new BaseHttpObserver<BaseResp>() {
                @Override
                protected void handleResultOkResp(BaseResp resp) {
                    finishUI();
                    ToastUtil.showToast("修改成功");
                    EvenBusUtil.updateCustormList();

                }
            });
        }
        else {
            CustomModel.addCustorm((IBaseView) getActivity(), data, new BaseHttpObserver<BaseResp>() {
                @Override
                protected void handleResultOkResp(BaseResp resp) {
                    finishUI();
                    ToastUtil.showToast("创建成功");
                    EvenBusUtil.updateCustormList();

                }
            });
        }


    }

    private void finishUI(){
        ViewUtil.finishFragment(getActivity(), this);
    }
}
